#include "ScheduledItemsDialog.h"
#include "ui_ScheduledItemsDialog.h"

ScheduledItemsDialog::ScheduledItemsDialog(QWidget *parent, AdminData *data) :QDialog(parent), ui(new Ui::ScheduledItemsDialog)
{
    this->setModal(true);
    ui->setupUi(this);
    this->data = data;

    ui->scheduledItemTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->scheduledItemTable->setColumnCount(2);
    QStringList headerList;
    headerList << "Name" << "Date";
    ui->scheduledItemTable->setHorizontalHeaderLabels(headerList);
}

ScheduledItemsDialog::~ScheduledItemsDialog()
{
    delete ui;
}

Ui::ScheduledItemsDialog* ScheduledItemsDialog::getUi() const
{
    return ui;
}

void ScheduledItemsDialog::on_addItemButton_clicked()
{
//    if (data->getSemDuration().at(i).isInRangeOf(ui->itemDateEdit->date()))
//    {
        for (int i = 0; i < data->getDaysOff().count(); ++i)
        {
            if (data->getDaysOff().at(i).isInRangeOf(ui->itemDateEdit->date()))
            {
                AdminErrorDialog *errorDialog = new AdminErrorDialog(this, "The date of the scheduled item confilicts with a holiday. Please change the evalutation day.");
                errorDialog->setModal(true);
                errorDialog->exec();
                break;
            }
            else
            {
                QString name = ui->itemNameEdit->text();
                QString date = ui->itemDateEdit->text();
                QTableWidgetItem *nameItem = new QTableWidgetItem; nameItem->setText(name);
                QTableWidgetItem *dateItem = new QTableWidgetItem; dateItem->setText(date);
                int row = ui->scheduledItemTable->rowCount();
                ui->scheduledItemTable->insertRow(row);
                ui->scheduledItemTable->setItem(row, 0, nameItem);
                ui->scheduledItemTable->setItem(row, 1, dateItem);
            }
        }
//    }
//    else
//    {

//    }

}

void ScheduledItemsDialog::on_removeItemButton_clicked()
{
    QList<QTableWidgetSelectionRange> ranges = ui->scheduledItemTable->selectedRanges();

    if (ranges.isEmpty())
    {
        return;
    }
    int begin = ranges.at(0).topRow(), count = ranges.at(0).rowCount();

    do
    {
        ui->scheduledItemTable->removeRow(begin);
    }while (--count);
}

void ScheduledItemsDialog::on_cancelButton_clicked()
{
    this->close();
}

void ScheduledItemsDialog::on_saveButton_clicked()
{
    for (int i = 0; i < ui->scheduledItemTable->rowCount(); ++i)
    {
        ((outlineSectionPage*)this->parentWidget())->getUi()->instructorTextEdit->append(ui->scheduledItemTable->item(i, 0)->text() +" \t " + ui->scheduledItemTable->item(i, 1)->text() + "\n");
    }
    this->close();
}
